# -*- coding: utf-8 -*-
#    Copyright (C) 2012  Ruslan V. Us <unclerus@gmail.com>, Aleksandr V. Bannov <kokudzin@gmail.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import time
class Request (object):
    '''
    Пользовательский запрос
    '''
    def __init__ (self, ip, aTime, url, userAgent):
        self.ip = ip
        self.aTime = aTime
        self.url = url
        self.userAgent = userAgent

    def __str__ (self):
        return '<Request ip: {ip}, aTime: {aTime}, url: {url}, userAgent: {userAgent}>'.format (
            ip = self.ip,
            aTime = self.aTime,
            url = self.url,
            userAgent = self.userAgent
        )

    def __repr__ (self):
        return self.__str__ ()

class Client (object):
    '''
    Класс клиента, хранит статистику
    '''
    def __init__ (self, ip, settings):
        self.ip = ip
        self.count = 0
        self.start = None
        self.urls = {}
        self.frequency = 0.0
        self.userAgents = {}
        self.banned = False
        self.settings = settings
        self.lastRequest = None
        self.stat = []

    def _inc (self, d, k):
        if k is None:
            k = '__unknown__'
        if k in d:
            d [k] += 1
        else:
            d [k] = 1

    def registerActivity (self, request):
        self.count += 1
        if not self.start:
            self.start = request.aTime
        else:
            period = request.aTime - self.start        
            self.frequency = self.count / period if period else 1
        self.lastRequest = request.aTime
        self._inc (self.urls, request.url)
        self._inc (self.userAgents, request.userAgent)
        self.stat.append ('url: {0} time: {1}'.format (request.url, time.strftime ('%Y-%m-%d %H:%M:%S', time.gmtime (request.aTime))))

    def isBot (self):
        return (self.frequency > self.settings.diffUrlFrequency and self.count > self.settings.diffUrlCount) \
            or (self.frequency >= self.settings.oneUrlFrequency and len (self.urls) == 1 and self.count > self.settings.oneUrlCount) \
            or (self.frequency >= self.settings.frequency and len (self.userAgents) > self.settings.userAgents)

    def __str__ (self):
        return '<ip: {0}, count: {4}, frequency: {1:0.3f}, urls: {2}, user-agents: {3}>'.format (self.ip, self.frequency, len (self.urls), len (self.userAgents), self.count)

    def __repr__ (self):
        return self.__str__ ()
